﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using ElectronicCommerce.Framework.Diagnostics;
using ElectronicCommerce.Framework.Resources;

namespace ElectronicCommerce.Framework.Utility
{
    /// <summary>
    /// 
    /// </summary>
    public class XmlUtils
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="files"></param>
        /// <param name="fileContentMergeFail"></param>
        /// <returns></returns>
        public static FileMergeResult MergeFiles(List<string> files)
        {
            Check.Argument.IsNotNull("files", files);
            Check.Argument.IsTrue(files.Count > 0, "files", R.FileArrayEmpty);

            FileMergeResult mergeResult = new FileMergeResult(files);
            XmlDocument doc = new XmlDocument();
            if (files.Count > 0)
            {
                string fileToMerge = string.Empty;
                for (int i = 0; i < files.Count; i++)
                {
                    fileToMerge = files[i];
                    try
                    {
                        if (!File.Exists(fileToMerge))
                        {
                            ThrowHelper.ThrowFileNotFoundException(R.FileNotFound, fileToMerge);
                        }

                        if (doc.DocumentElement == null)
                        {
                            doc.Load(fileToMerge);
                        }
                        else
                        {
                            XmlDocument docTemp = new XmlDocument();
                            docTemp.Load(fileToMerge);
                            foreach (XmlNode node in docTemp.DocumentElement.ChildNodes)
                            {
                                XmlNode nodeTemp = doc.ImportNode(node, true);
                                doc.DocumentElement.AppendChild(nodeTemp);
                            }
                        }

                        mergeResult.FilesMerged.Add(fileToMerge);
                    }
                    catch (FileNotFoundException ex)
                    {
                        mergeResult.AddFileMergeFailReason(fileToMerge, ex);
                    }
                    catch (XmlException ex)
                    {
                        mergeResult.AddFileMergeFailReason(fileToMerge, string.Format(R.FileLoadFail_Xml, fileToMerge), ex);
                    }
                    catch (FileLoadException ex)
                    {
                        mergeResult.AddFileMergeFailReason(fileToMerge, string.Format(R.FileLoadFail_Xml, fileToMerge), ex);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            mergeResult.FileContentMerged = doc.OuterXml;

            return mergeResult;
        }

        /// <summary>
        /// 从DataSet Xml 配置文件中获取 DataTable.
        /// </summary>
        /// <param name="file">完全限定路径配置文件。</param>
        /// <returns>DataTable数据集。</returns>
        public static DataTable LoadXmlDataTable(string file)
        {
            // if file does not exist, no log
            if (!File.Exists(file))
            {
                return null;
            }

            try
            {
                DataSet ds = new DataSet();
                XmlReadMode mode = ds.ReadXml(file, XmlReadMode.ReadSchema);
                return ds.Tables[0];
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 从DataSet Xml 配置文件中获取 DataTable.
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public static DataTable LoadXmlDataTable(string[] files)
        {
            if (files == null || files.Length == 0)
            {
                return null;
            }

            DataTable dt = new DataTable();
            foreach (string file in files)
            {
                DataSet ds = new DataSet();
                ds.ReadXml(file, XmlReadMode.ReadSchema);
                if (ds.Tables.Count > 0)
                {
                    dt.Merge(ds.Tables[0]);
                }
            }

            return dt;
        }
    }
}
